﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ShapeDrawer
{
    public class Toolbar : StackPanel
    {
        private bool hasActivatedElement;
        private IList<Icon> icons;

        public Toolbar()
        {
            this.Width = 40;
            this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            this.Background = new SolidColorBrush(Color.FromArgb(64, 2, 236, 255));

            this.hasActivatedElement = false;
            this.icons = new List<Icon>();
            this.CreateToolIcons();
        }

        private void AddElement(Icon icon)
        {
            this.icons.Add(icon);
            this.Children.Add(icon.Frame);
        }

        private void CreateToolIcons()
        {
            Icon drawCircleIcon = IconMaker.MakeDrawCircleIcon();
            drawCircleIcon.Frame.Margin = new Thickness(0, 40, 0, 0);
            drawCircleIcon.Frame.MouseLeftButtonUp += DrawCircleClick;
            this.AddElement(drawCircleIcon);

            Icon drawPolygonIcon = IconMaker.MakeDrawPolygonIcon();
            drawPolygonIcon.Frame.MouseLeftButtonUp += DrawPolygonClick;
            this.AddElement(drawPolygonIcon);

            Icon zoomIcon = IconMaker.MakeZoomIcon();
            zoomIcon.Frame.MouseLeftButtonUp += ZoomIconClick;
            this.AddElement(zoomIcon);

            Icon deleteIcon = IconMaker.MakeDeleteIcon();
            deleteIcon.Frame.MouseLeftButtonUp += DeleteIconClick;
            this.AddElement(deleteIcon);
        }

        void DeleteIconClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShapeDrawing.Mode = DrawingMode.Delete;
            this.RefreshActivations((Rectangle)sender);
        }

        void ZoomIconClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShapeDrawing.Mode = DrawingMode.Zoom;
            this.RefreshActivations((Rectangle)sender);
        }

        private void DrawCircleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.RefreshActivations((Rectangle)sender);
            ShapeDrawing.Mode = DrawingMode.Ellipse;
        }

        private void DrawPolygonClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.RefreshActivations((Rectangle)sender);
            ShapeDrawing.Mode = DrawingMode.Polygon;
        }

        public void RefreshActivations(Rectangle iconFrame)
        {
            foreach (var item in this.icons)
            {
                if (item.Frame != iconFrame)
                {
                    item.State = State.Normal;
                }
            }
        }
    }
}
